/*
** Copyright (C) 2010 Calin Radoni
**
** This file is part of CHScanner.
**
** CHScanner is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** CHScanner is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with CHScanner. If not, see <http://www.gnu.org/licenses/>.
*/

// DlgWmiSel.cpp : implementation file
//

#include "stdafx.h"
#include "CHScanner.h"
#include "DlgWmiSel.h"

//---------------------------------------------------------------------------

IMPLEMENT_DYNAMIC(DlgWmiSel, CRgnDialog)

DlgWmiSel::DlgWmiSel(CWnd* pParent /*=NULL*/) : CRgnDialog(DlgWmiSel::IDD, pParent)
{
	skinIn = NULL;
	retVal = false;
	retSelection = 0;
}

DlgWmiSel::~DlgWmiSel()
{
}

//---------------------------------------------------------------------------

void DlgWmiSel::DoDataExchange(CDataExchange* pDX)
{
	CRgnDialog::DoDataExchange(pDX);

	DDX_Control(pDX, IDC_STA_WMITITLE, staTitle);
	DDX_Control(pDX, IDC_CHECK1, btnChk1);
	DDX_Control(pDX, IDC_CHECK2, btnChk2);
	DDX_Control(pDX, IDC_CHECK3, btnChk3);
	DDX_Control(pDX, IDC_CHECK4, btnChk4);
	DDX_Control(pDX, IDC_CHECK5, btnChk5);
	DDX_Control(pDX, IDC_CHECK6, btnChk6);
	DDX_Control(pDX, IDC_CHECK7, btnChk7);
	DDX_Control(pDX, IDC_CHECK8, btnChk8);
	DDX_Control(pDX, IDC_CHECK9, btnChk9);
	DDX_Control(pDX, IDC_CHECK10, btnChk10);
	DDX_Control(pDX, IDC_CHECK11, btnChk11);
	DDX_Control(pDX, IDC_CHECK12, btnChk12);
	DDX_Control(pDX, IDC_CHECK13, btnChk13);
	DDX_Control(pDX, IDC_BTN_OK, btnOk);
	DDX_Control(pDX, IDC_BTN_CANCEL, btnCancel);
}

//---------------------------------------------------------------------------

BEGIN_MESSAGE_MAP(DlgWmiSel, CRgnDialog)
	ON_BN_CLICKED(IDC_BTN_CANCEL, &DlgWmiSel::OnBnClickedBtnCancel)
	ON_BN_CLICKED(IDC_BTN_OK, &DlgWmiSel::OnBnClickedBtnOk)
END_MESSAGE_MAP()

//---------------------------------------------------------------------------

BOOL DlgWmiSel::OnInitDialog()
{
	CRgnDialog::OnInitDialog();

	SetProperties(skinIn, false, _T("WMI"));

	if(skin!=NULL){
		staTitle.textColor = skin->textColor;
		btnChk1.textColor  = skin->textColor;
		btnChk2.textColor  = skin->textColor;
		btnChk3.textColor  = skin->textColor;
		btnChk4.textColor  = skin->textColor;
		btnChk5.textColor  = skin->textColor;
		btnChk6.textColor  = skin->textColor;
		btnChk7.textColor  = skin->textColor;
		btnChk8.textColor  = skin->textColor;
		btnChk9.textColor  = skin->textColor;
		btnChk10.textColor = skin->textColor;
		btnChk11.textColor = skin->textColor;
		btnChk12.textColor = skin->textColor;
		btnChk13.textColor = skin->textColor;
	}

	WorkCfgToData();

	UpdateData(FALSE);

	return TRUE;  // return TRUE unless you set the focus to a control
}

//---------------------------------------------------------------------------

void DlgWmiSel::OnOK()
{
	// CRgnDialog::OnOK();
}

void DlgWmiSel::OnCancel()
{
	// CRgnDialog::OnCancel();
}

//---------------------------------------------------------------------------

void DlgWmiSel::OnBnClickedBtnOk()
{
	UpdateData(TRUE);
	WorkCfgFromData();

	retVal = true;
	EndDialog(IDOK);
}

void DlgWmiSel::OnBnClickedBtnCancel()
{
	retVal = false;
	EndDialog(IDABORT);
}

//---------------------------------------------------------------------------

void DlgWmiSel::WorkCfgToData(void)
{
	btnChk1.SetCheck(retSelection & 0x0001 ? BST_CHECKED : BST_UNCHECKED);
	btnChk2.SetCheck(retSelection & 0x0002 ? BST_CHECKED : BST_UNCHECKED);
	btnChk3.SetCheck(retSelection & 0x0004 ? BST_CHECKED : BST_UNCHECKED);
	btnChk4.SetCheck(retSelection & 0x0008 ? BST_CHECKED : BST_UNCHECKED);

	btnChk5.SetCheck(retSelection & 0x0010 ? BST_CHECKED : BST_UNCHECKED);
	btnChk6.SetCheck(retSelection & 0x0020 ? BST_CHECKED : BST_UNCHECKED);
	btnChk7.SetCheck(retSelection & 0x0040 ? BST_CHECKED : BST_UNCHECKED);
	btnChk8.SetCheck(retSelection & 0x0080 ? BST_CHECKED : BST_UNCHECKED);

	btnChk9.SetCheck( retSelection & 0x0100 ? BST_CHECKED : BST_UNCHECKED);
	btnChk10.SetCheck(retSelection & 0x0200 ? BST_CHECKED : BST_UNCHECKED);
	btnChk11.SetCheck(retSelection & 0x0400 ? BST_CHECKED : BST_UNCHECKED);
	btnChk12.SetCheck(retSelection & 0x0800 ? BST_CHECKED : BST_UNCHECKED);

	btnChk13.SetCheck(retSelection & 0x1000 ? BST_CHECKED : BST_UNCHECKED);
}

void DlgWmiSel::WorkCfgFromData(void)
{
	retSelection = 0;

	if(btnChk1.GetCheck()==BST_CHECKED) retSelection |= 0x0001;
	if(btnChk2.GetCheck()==BST_CHECKED) retSelection |= 0x0002;
	if(btnChk3.GetCheck()==BST_CHECKED) retSelection |= 0x0004;
	if(btnChk4.GetCheck()==BST_CHECKED) retSelection |= 0x0008;

	if(btnChk5.GetCheck()==BST_CHECKED) retSelection |= 0x0010;
	if(btnChk6.GetCheck()==BST_CHECKED) retSelection |= 0x0020;
	if(btnChk7.GetCheck()==BST_CHECKED) retSelection |= 0x0040;
	if(btnChk8.GetCheck()==BST_CHECKED) retSelection |= 0x0080;

	if(btnChk9.GetCheck()==BST_CHECKED)  retSelection |= 0x0100;
	if(btnChk10.GetCheck()==BST_CHECKED) retSelection |= 0x0200;
	if(btnChk11.GetCheck()==BST_CHECKED) retSelection |= 0x0400;
	if(btnChk12.GetCheck()==BST_CHECKED) retSelection |= 0x0800;

	if(btnChk13.GetCheck()==BST_CHECKED) retSelection |= 0x1000;
}

//---------------------------------------------------------------------------

void DlgWmiSel::ResizeChilds(void)
{
	int bw, bh;		///< size of button controls
	int lw, lh;		///< size of label controls
	int ew, eh;		///< size of edit and combobox controls
	int ml;			///< for vertical-center-aligning the labels with edit and combo controls
	int tw;			///< total required width
	int cw, ch;		///< client region's width and height
	int mx, my;		///< left and top space for displaying the objects in the center of the window
	int dd;			///< space between objects
	RECT rect;
	int posx1, posx2, posy;

	dd = 2;

	btnOk.GetWindowRect(&rect);
	bw = rect.right - rect.left;
	bh = rect.bottom - rect.top;

	staTitle.GetWindowRect(&rect);
	lw = rect.right - rect.left;

	btnChk1.GetWindowRect(&rect);
	//ew = rect.right - rect.left;
	ew = 170;
	eh = rect.bottom - rect.top;

	lh = eh;

	///< tw is computed according to the layout
	tw = ew + dd + ew;

	cw = clientRect.right - clientRect.left;
	ch = clientRect.bottom - clientRect.top;

	/*
	CString str;
	str.Format(_T("%d x %d\n"), cw, ch);
	OutputDebugString(str);
	*/

	mx = dd;
	my = dd;

	posx1 = clientRect.left + mx;
	posx2 = posx1 + dd + ew;
	posy = clientRect.top + my;
	ml    = dd + eh;		// ml is reused here

	staTitle.MoveWindow((tw - lw)/2, posy, lw, lh, TRUE);
	posy += ml;

	btnChk1.MoveWindow(posx1, posy, ew, eh, TRUE);
	btnChk7.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += ml;
	btnChk2.MoveWindow(posx1, posy, ew, eh, TRUE);
	btnChk8.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += ml;
	btnChk3.MoveWindow(posx1, posy, ew, eh, TRUE);
	btnChk9.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += ml;
	btnChk4.MoveWindow( posx1, posy, ew, eh, TRUE);
	btnChk10.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += ml;
	btnChk5.MoveWindow( posx1, posy, ew, eh, TRUE);
	btnChk11.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += ml;
	btnChk6.MoveWindow( posx1, posy, ew, eh, TRUE);
	btnChk12.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += ml;
	btnChk13.MoveWindow(posx1, posy, ew, eh, TRUE);
	//btnChkXX.MoveWindow(posx2, posy, ew, eh, TRUE);
	posy += ml;

	posx2 = cw - bw - dd;
	posx1 = posx2 - bw - dd;

	btnOk.MoveWindow(posx1, posy, bw, bh, TRUE);
	btnCancel.MoveWindow(posx2, posy, bw, bh, TRUE);
}

bool DlgWmiSel::GetMinimumSize(RECT* rectIn)
{
	if(rectIn==NULL) return false;

	rectIn->left   = 0;
	rectIn->right  = 346;
	rectIn->top    = 0;
	rectIn->bottom = 172;

	return true;
}

//---------------------------------------------------------------------------
